//
//  ZFWeiBoViewController.swift
//  WeiBoAPP
//
//  Created by ios001 on 2021/6/11.
//  Copyright © 2021 zxf. All rights reserved.
//

import UIKit
/**
 定义全局常亮
 */
private let cellId = "cellID"
private let retweetcellId = "retweetcellId" //被转发微博cell


class ZFWeiBoViewController: ZFBaseViewController {

    
    private lazy var statusList = [String]()
    private lazy var listViewModel = WBStatusViewModel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.random()

        // Do any additional setup after loading the view.
    }
    
    //加载数据
    override func loadData() {
        
        print("开始加载数据")
        
        listViewModel.loadStatus(pullUp:self.isPullup) { (isSuccess) in
            self.refreshController?.endRefreshing()
            self.isPullup = false
            self.tableView?.reloadData()
        }
    }
    
    override func refreshData() {
        self.loadData()
//        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 3, execute: {
//            self.loadData()
//            self.refreshController?.endRefreshing()
//            self.isPullup = false
//        })
    }
    
    //显示好友
    @objc private func showFriends ()
    {
        print("显示好友")
        let vc  = ZFDomeViewController()
        navigationController?.pushViewController(vc, animated: true)
        
        
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
//
extension ZFWeiBoViewController {
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listViewModel.statusList.count //statusList.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! WBCell
        
        let cell  = tableView.dequeueReusableCell(withIdentifier: retweetcellId, for: indexPath) as! WBCell;
        
        
//        cell.signLabel?.text = listViewModel.statusList[indexPath.row].status.user?.sigin
        cell.viewModel = listViewModel.statusList[indexPath.row]
        return cell
        
    }
}

extension ZFWeiBoViewController {
    
    override func setupNavUI() {
        super.setupNavUI()

        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", target: self, action: #selector(showFriends))
        
        setupNavTitle()
    }
    
    override func setupTableView() {
        super.setupTableView()
        
//        tableView?.register(UITableViewCell.self , forCellReuseIdentifier: cellId)
        tableView?.register(UINib(nibName: "WBStatueNormalCell", bundle: nil), forCellReuseIdentifier: cellId)
        tableView?.register(UINib(nibName: "WBStatueRetweeteCell", bundle: nil), forCellReuseIdentifier: retweetcellId)
        
        tableView?.separatorStyle = .none;
        tableView?.rowHeight = UITableViewAutomaticDimension;
        tableView?.estimatedRowHeight = 300;
    }
    
    private func setupNavTitle() {
        
        let button = UIButton.init(type: UIButton.ButtonType.custom)
        button.setTitle("微博*还好", for: .normal)
        button.setTitleColor(UIColor.black, for: UIControl.State.normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 17)
        button.addTarget(self, action: #selector(titleClickButton), for: .touchUpInside)
//        button.frame = CGRect(x: 0, y: 0, width: 100, height: 44)
        navItem.titleView = button
    }
    
    @objc func titleClickButton (btn:UIButton) {
        btn.isSelected = !btn.isSelected
        
    }
}


extension ZFWeiBoViewController {
    
    func setupImage ()  {
        let iv = UIImageView.init(image: UIImage(named: "login_service_userName"))
        iv.frame = CGRect(x: 20, y: 100, width: 100, height: 100)
        iv.backgroundColor = UIColor.black
        view.addSubview(iv)
        //JPG图像需要添加后缀
        let image = UIImage(named: "login_service_userName")
        iv.image = avatarImage(image: image!, size: iv.size,backColor: view.backgroundColor)
        
    }
    
    /****/
    
    //将制定的图像进行拉伸，并返回新的图像
    func avatarImage(image: UIImage,size:CGSize,backColor:UIColor?) -> UIImage {
    
        let rect  = CGRect(origin: CGPoint(), size: size)
        
        //上下文
        /**
         参数：
         size： 绘图的尺寸
         不透明：false（透明） true （不透明）
         scale：屏幕分辨率，默认生成的图像默认使用1.0的分辨率，图片质量不好，可以指定 0 ，会选择当前设备的屏幕分辨率。
         **/
        UIGraphicsBeginImageContextWithOptions(rect.size, true, 0)
        //背景填充
        backColor?.setFill()
        UIRectFill(rect)
        //实例化一个圆形的路径
        let path  = UIBezierPath(ovalIn: rect)
        //路径裁剪，后续的绘图，都会出现在圆形路径内部，外部全部干掉
        path.addClip()
        
        //绘图，drawInRect 就是指定区域内拉伸屏幕
        image.draw(in: rect)
        
        //绘制内部的圆形
        UIColor.darkGray.setStroke()
        path.lineWidth = 2
        path.stroke()
        
        
        let result = UIGraphicsGetImageFromCurrentImageContext()
        //关闭上下文
        UIGraphicsEndImageContext()
        
        return result!
    }
    
}
